<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :xs="24" :sm="24" :md="10" :lg="10" :xl="10">
        <el-card class="box-card">
          <eHeader :param-bean="paramBean" :dicts="dicts" :sup_this="sup_this"/>
          <!--表格渲染-->
          <tree-table v-loading="loading" :expand-all="expand" :data="data" :columns="columns" size="small">
            <!--<el-table-column label="组织类别" align="center">
              <template slot-scope="scope">
                <div v-for="item in dicts.org_type" :key="item.id">
                  <el-tag v-if="scope.row.orgType.toString() === item.value" :type="scope.row.orgType ? '' : 'info'">{{ item.label }}</el-tag>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="状态" align="center">
              <template slot-scope="scope">
                <div v-for="item in dicts.org_status" :key="item.id">
                  <el-tag v-if="scope.row.orgStatus.toString() === item.value" :type="scope.row.orgStatus ? '' : 'info'">{{ item.label }}</el-tag>
                </div>
              </template>
            </el-table-column>-->
            <el-table-column v-if="checkPermission(['SYS_ADMIN','ORGSET_ALL','ORGSET_EDIT','ORGSET_DELETE'])" label="操作" width="200" align="center">
              <template slot-scope="scope">
                <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="viewUser(scope.row)">查看</el-button>
                <edit v-permission="['SYS_ADMIN','ORGSET_ALL','ORGSET_EDIT']" :dicts="dicts" :data="scope.row" :sup_this="sup_this"/>
                <el-popover
                  v-permission="['SYS_ADMIN','ORGSET_ALL','ORGSET_DELETE']"
                  :ref="scope.row.id"
                  placement="top"
                  width="180">
                  <p>确定删除本条数据吗？</p>
                  <div style="text-align: right; margin: 0">
                    <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                    <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
                  </div>
                  <el-button slot="reference" :disabled="scope.row.id === 1" type="danger" icon="el-icon-delete" size="mini"/>
                </el-popover>
              </template>
            </el-table-column>
          </tree-table>
        </el-card>
      </el-col>

      <el-col :xs="24" :sm="24" :md="14" :lg="14" :xl="14">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>{{ orgName }} 用户</span>
            <div v-permission="['SYS_ADMIN','USER_ORGSET_ALL','USER_ORGSET_CREATE']" style="display: inline-block;margin: 0px 2px;">
              <el-button
                class="filter-item"
                size="mini"
                type="primary"
                icon="el-icon-plus"
                @click="addUser">增加用户</el-button>
              <userSelectList ref="userSelectList" :sup_this="sup_this" />
            </div>
            <userOrgSetList ref="userOrgSetList" :sup_this="sup_this"/>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import treeTable from '@/components/TreeTable'
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { del, loadDataUrl, saveUserOrgSet } from '@/api/orgSet'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import edit from './module/edit'
import userOrgSetList from './module/userOrgSet'
import userSelectList from '../user/module/userSelectList'

export default {
  components: { eHeader, edit, treeTable, userOrgSetList, userSelectList },
  mixins: [initData, initDict],
  data() {
    return {
      columns: [
        {
          text: '名称',
          value: 'orgName'
        },
        {
          text: '编号',
          value: 'thirdCode'
        }
      ],
      orgInfo: {},
      orgName: '',
      delLoading: false, sup_this: this, expand: true
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    async initDataDict() {
      await this.getDictByType('org_status')
      await this.getDictByType('enable_flag')
      await this.getDictByType('org_type')
    },
    beforeInit() {
      this.url = loadDataUrl()
      if (!this.dicts.lan_code) {
        this.initDataDict()
      }
      if (this.$refs.userOrgSetList) {
        this.$refs.userOrgSetList.data.rtnList = []
        this.$refs.userOrgSetList.orgCode = ''
      }
      return true
    },
    afterInit(res) {
      this.orgName = ''
    },
    viewUser(val) {
      debugger
      if (val) {
        this.$refs.userOrgSetList.orgCode = val.orgCode
        this.orgName = val.orgName
        this.orgInfo = val
        this.$refs.userOrgSetList.init()
      }
    },
    doUserSelectCallback(selectedRows, userSelectObj) {
      const data = { userOrgSetVOList: [] }
      selectedRows.forEach(row => {
        const userOrgSetVo = {}
        userOrgSetVo.userId = row.id
        userOrgSetVo.userAccount = row.userAccount
        userOrgSetVo.cpyCode = this.orgInfo.cpyCode
        userOrgSetVo.orgCode = this.orgInfo.orgCode
        data.userOrgSetVOList.push(userOrgSetVo)
      })
      saveUserOrgSet(data).then(res => {
        userSelectObj.cancel()
        userSelectObj.loading = false
        this.$refs.userOrgSetList.init()
      }).catch(err => {
        userSelectObj.loading = false
        console.log(err)
      })
    },
    addUser() {
      if (!this.orgName) {
        this.$notify({
          title: '请选择对应的组织机构',
          type: 'success',
          duration: 2000
        })
        return
      }
      const _this = this.$refs.userSelectList
      _this.init()
      _this.dialog = true
    },
    subDelete(id) {
      this.delLoading = true
      const data = { idList: [{ id: id }] }
      del(data).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
